<?php
class RuleStaffTypesController extends AppController {

	var $name = 'RuleStaffTypes';
	var $components = array('Auth');

	function admin_index() {
		$this->RuleStaffType->recursive = 0;
		$this->set('ruleStaffTypes', $this->paginate());
	}

	function admin_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid rule staff type', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ruleStaffType', $this->RuleStaffType->read(null, $id));
	}

	function admin_add() {
		if (!empty($this->data)) {
			$this->RuleStaffType->create();
			if ($this->RuleStaffType->save($this->data)) {
				$this->Session->setFlash(__('The rule staff type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rule staff type could not be saved. Please, try again.', true));
			}
		}
		$staffTypes = $this->RuleStaffType->StaffType->find('list');
		$rules = $this->RuleStaffType->Rule->find('list');
		$this->set(compact('staffTypes', 'rules'));
	}

	function admin_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid rule staff type', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->RuleStaffType->save($this->data)) {
				$this->Session->setFlash(__('The rule staff type has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The rule staff type could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->RuleStaffType->read(null, $id);
		}
		$staffTypes = $this->RuleStaffType->StaffType->find('list');
		$rules = $this->RuleStaffType->Rule->find('list');
		$this->set(compact('staffTypes', 'rules'));
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for rule staff type', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->RuleStaffType->delete($id)) {
			$this->Session->setFlash(__('Rule staff type deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Rule staff type was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
	function allow($rule_id){
		$staff_type_id = $this->Auth->user("staff_type_id");
		return ($this->RuleStaffType->find('count', array('conditions' => array('RuleStaffType.staff_type_id' => $staff_type_id,'RuleStaffType.rule_id' => $rule_id))) >0);
	}
}
?>